package com.shuangti.order.service.impl;

import com.shuangti.order.bean.OrderTbl;
import com.shuangti.order.feign.StorageFeignClient;
import com.shuangti.order.mapper.OrderTblMapper;
import com.shuangti.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderTblMapper orderTblMapper;

    @Autowired
    StorageFeignClient storageFeignClient;


    @Transactional
    @Override
    public OrderTbl create(Long user_id, long product_id,int count,String shipping_address) {
        //得到商品价格
        BigDecimal price = storageFeignClient.getPrice(product_id);
        //计算总额
        BigDecimal total_amount = price.multiply(BigDecimal.valueOf(count));

        //保存订单
        OrderTbl orderTbl = new OrderTbl();
        orderTbl.setUser_id(user_id);
        orderTbl.setProduct_id(product_id);
        orderTbl.setTotal_amount(total_amount);
        orderTbl.setCreated_at(LocalDateTime.now());
        orderTbl.setShipping_address(shipping_address);

        orderTblMapper.insert(orderTbl);

        return orderTbl;
    }
}
